package com.java.wmovie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.wmovie.entity.*;
import com.java.wmovie.mapper.*;
import com.java.wmovie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhw
 * @since 2022-01-27
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    File savePos = new File("src/main/resources/static/img/upload");
    @Autowired
    MovieService movieService;
    @Autowired
    MovieMapper movieMapper;
    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrdersService ordersService;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    SliceService sliceService;
    @Autowired
    SliceMapper sliceMapper;
    @Autowired
    LocationMapper locationMapper;
    @Autowired
    SiteMapper siteMapper;
    @Autowired
    CinemaMapper cinemaMapper;



    @RequestMapping("/index")
    public String index(Model model){
        PageUtils pageUtils = new PageUtils(new Page(1, 8), "/admin/movie");
        Page<Movie> page = movieMapper.selectPage(pageUtils.getPage(),null);
        model.addAttribute("page",page);
        PageUtils pageUtils1 = new PageUtils(new Page(1, 8), "/admin/user");
        Page<User> page1 = userMapper.selectPage(pageUtils1.getPage(),null);
        model.addAttribute("page1",page1);
        PageUtils pageUtils2 = new PageUtils(new Page(1, 10), "/admin/order");
        Page<Orders> page2 = ordersMapper.selectPage(pageUtils2.getPage(),null);
        model.addAttribute("page2",page2);
        return "/admin/index";
    }

    @RequestMapping("/movie")
    public String movie(Model model, HttpSession session, @RequestParam(value = "i",defaultValue = "1")Long i){
        PageUtils pageUtils = new PageUtils(new Page(i, 8), "/admin/movie");
        Page<Movie> page = movieMapper.selectPage(pageUtils.getPage(),null);
        model.addAttribute("page",page);

        return "/admin/movie";
    }

    @RequestMapping("/movie/new")
    public String MovieNew(){
        return "/admin/addmovie";
    }

    @RequestMapping("/movie/save")
    public String MovieSave(Model model, Movie movie, @RequestParam("name")String name, @RequestParam("types")String types,
                            @RequestParam("region")String region, @RequestParam("picture")String picture, @RequestParam("file1") MultipartFile file1,
                            @RequestParam("content") String content, @RequestParam("price")BigDecimal price,
                            @RequestParam("duration")Integer duration, @RequestParam("onlin")String onlin) throws ParseException {
        try {
            String realPath = savePos.getCanonicalPath();
            // 上传该文件/图像至该文件夹下
            file1.transferTo(new File(realPath+"/"+file1.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
        Movie movie1=movieMapper.selectOne(new QueryWrapper<Movie>().eq("name",name).eq("types",types).eq("region",region).eq("picture",picture)
                .eq("content",content).eq("price",price).eq("duration",duration)
                .eq("onlin",onlin).eq("img",file1.getOriginalFilename()));
        if(ObjectUtils.isEmpty(movie1)){
            if (name == null){
                model.addAttribute("msg2","不能有空值");
                return "/admin/addmovie";
            }else {
                movie.setImg(file1.getOriginalFilename());
                movieService.save(movie);
                return "redirect:/admin/movie";
            }
        }else {
            model.addAttribute("msg2","项目已存在");
            return "/admin/addmovie";
        }

    }

    @RequestMapping("/movie/by")
    public String MovieBy(@RequestParam("name")String name,Model model,HttpSession session){
        PageUtils pageUtils=new PageUtils(new Page(1,1),"/admin/movie");
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        Page<Movie> page=movieMapper.selectPage(pageUtils.getPage(),wrapper);
        System.out.println(page.getRecords());
        model.addAttribute("page",page);
        return "/admin/movie";
    }

    @RequestMapping("/movie/delete")
    public String delete(Model model, HttpSession session, Movie movie, @RequestParam("id") Integer id){
        boolean b=movieService.removeById(id);
        System.out.println("删除----->"+b);
        return "redirect:/admin/movie";
    }

    @RequestMapping("/movie/updates")
    public String updates(HttpSession session,Model model,@RequestParam("id") Integer id){
        session.setAttribute("mId",id);
        System.out.println("进入修改界面");
        Movie movie=movieService.getById(id);
        model.addAttribute("movie",movie);
        return "/admin/xgmovie";
    }

    @RequestMapping("/movie/update")
    public String update(Model model,HttpSession session,Movie movie,@RequestParam("file1") MultipartFile file1){
        try {
            String realPath = savePos.getCanonicalPath();
            // 上传该文件/图像至该文件夹下
            file1.transferTo(new File(realPath+"/"+file1.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
        Integer id1=(Integer) session.getAttribute("mId");
        movie.setMId(id1);
        movie.setImg(file1.getOriginalFilename());
        movieService.updateById(movie);
        System.out.println("修改完成");
        return "redirect:/admin/movie";
    }

    @RequestMapping("/password")
    public String password(Model model,@RequestParam(value = "i",defaultValue = "1")Integer i,HttpSession session){
        Admin admin=adminService.getById(i);
        session.setAttribute("a_id",i);
        model.addAttribute("admin",admin);
        System.out.println("进入管理员修改密码界面");
        return "/admin/admin";
    }

    @RequestMapping("/passwords")
    public String passwords(Admin admin,HttpSession session){
        Integer id1=(Integer) session.getAttribute("a_id");
        admin.setId(id1);
        adminService.updateById(admin);
        System.out.println("修改完成");
        return "redirect:/admin/password";
    }

    @RequestMapping("/user")
    public String user(Model model, HttpSession session, @RequestParam(value = "i",defaultValue = "1")Long i){
        PageUtils pageUtils = new PageUtils(new Page(i, 8), "/admin/user");
        Page<User> page = userMapper.selectPage(pageUtils.getPage(),null);
        model.addAttribute("page",page);
        return "/admin/user";
    }

    @RequestMapping("/user/new")
    public String UserNew(){
        return "/admin/adduser";
    }

    @RequestMapping("/user/save")
    public String save(Model model,@RequestParam("uName")String uName,@RequestParam("uPassword")String uPassword,@RequestParam("number")String number,@RequestParam("gender")String gender,HttpSession session,User user){
        User user1=userMapper.selectOne(new QueryWrapper<User>().eq("u_name",uName)
                .eq("u_password",uPassword).eq("gender",gender).eq("number",number));
        if(ObjectUtils.isEmpty(user1)){
            if (uName == null){
                model.addAttribute("msg2","不能有空值");
                return "/admin/adduser";
            }else {
                userService.save(user);
                return "redirect:/admin/user";
            }
        }else {
            model.addAttribute("msg2","项目已存在");
            return "/admin/adduser";
        }
    }

    @RequestMapping("/user/by")
    public String UserBy(@RequestParam("name")String name,Model model,HttpSession session){
        PageUtils pageUtils=new PageUtils(new Page(1,8),"/admin/user");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("u_name", name);
        Page<User> page=userMapper.selectPage(pageUtils.getPage(),wrapper);
        System.out.println(page.getRecords());
        model.addAttribute("page",page);
        return "/admin/user";
    }

    @RequestMapping("/user/delete")
    public String u_delete(Model model, HttpSession session, Movie movie, @RequestParam("id") Integer id){
        boolean b=userService.removeById(id);
        System.out.println("删除----->"+b);
        return "redirect:/admin/user";
    }

    @RequestMapping("/user/updates")
    public String u_updates(HttpSession session,Model model,@RequestParam("id") Integer id){
        session.setAttribute("uId",id);
        System.out.println("进入修改界面");
        User user=userService.getById(id);
        model.addAttribute("user",user);
        return "/admin/xguser";
    }

    @RequestMapping("/user/update")
    public String u_update(Model model,HttpSession session,User user){
        Integer id1=(Integer) session.getAttribute("uId");
        user.setUId(id1);
        userService.updateById(user);
        System.out.println("修改完成");
        return "redirect:/admin/user";
    }

    @RequestMapping("/order")
    public String order(Model model, HttpSession session, @RequestParam(value = "i",defaultValue = "1")Long i){
        PageUtils pageUtils = new PageUtils(new Page(i, 10), "/admin/order");
        Page<Orders> page = ordersMapper.selectPage(pageUtils.getPage(),null);
        model.addAttribute("page",page);
        return "/admin/order";
    }

    @RequestMapping("/order/by")
    public String OrderBy(@RequestParam("oId")Integer id,Model model,HttpSession session){
        PageUtils pageUtils=new PageUtils(new Page(1,1),"/admin/order");
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.like("o_id", id);
        Page<Orders> page=ordersMapper.selectPage(pageUtils.getPage(),wrapper);
        System.out.println(page.getRecords());
        model.addAttribute("page",page);
        return "/admin/order";
    }

    @RequestMapping("/slice")
    public String slice(Model model,HttpSession session,@RequestParam(value = "i",defaultValue = "1")Integer i){
        PageUtils pageUtils = new PageUtils(new Page(i, 8), "/admin/slice");
        Page<Slice> page = sliceMapper.selectPage(pageUtils.getPage(),null);
        model.addAttribute("page",page);
        return "/admin/slice";
    }

    @RequestMapping("/slice/by")
    public String SliceBy(Model model ,HttpSession session,@RequestParam("name")String name){
        PageUtils pageUtils=new PageUtils(new Page(1,8),"/admin/slice");
        QueryWrapper<Slice> wrapper=new QueryWrapper<>();
        wrapper.like("m_name",name);
        Page<Slice> page =sliceMapper.selectPage(pageUtils.getPage(),wrapper);
        model.addAttribute("page",page);
        return "/admin/slice";
    }

    @RequestMapping("/slice/new")
    public String SliceNew(Model model){
        QueryWrapper<Movie> wrapper=new QueryWrapper<>();
        wrapper.like("m_id","");
        List<Movie> movie=movieMapper.selectList(wrapper);
        model.addAttribute("movie",movie);

        QueryWrapper<Location> wrapper1=new QueryWrapper<>();
        wrapper1.like("l_id","");
        List<Location> location=locationMapper.selectList(wrapper1);
        model.addAttribute("location",location);

        QueryWrapper<Site> wrapper2=new QueryWrapper<>();
        wrapper1.like("s_id","");
        List<Site> sites=siteMapper.selectList(wrapper2);
        model.addAttribute("sites",sites);

        QueryWrapper<Cinema> wrapper3=new QueryWrapper<>();
        wrapper1.like("c_id","");
        List<Cinema> cinemas=cinemaMapper.selectList(wrapper3);
        model.addAttribute("cinemas",cinemas);
        return "/admin/addslice";
    }

    @RequestMapping("/slice/save")
    public String SliceSave(@RequestParam("mName")String mName,@RequestParam("lId")String lId,
                            Model model,@RequestParam("cinema")String cinema,@RequestParam("site")String site,
                            HttpSession session,Slice slice){
        Slice slice1=sliceMapper.selectOne(new QueryWrapper<Slice>().eq("m_name",mName)
                .eq("l_id",lId));
        sliceService.save(slice);
        return "redirect:/admin/slice";
    }

    @RequestMapping("/slice/delete")
    public String SliceDelete(@RequestParam("id")Integer id){
        boolean b=sliceService.removeById(id);
        System.out.println("删除----》"+b);
        return "redirect:/admin/slice";
    }



}

